﻿
Imports WINX32Lib 'Remember to add r3eference first (Add Reference option). For MS Visual Studio .NET 2003: before adding a reference to the project, corresponding assembly should be added to the Add Reference dialog box (http://msdn.microsoft.com/en-us/library/wkze6zky%28v=vs.71%29.aspx).
Imports System.IO
Imports System.Math

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents TextBox_File_Name As System.Windows.Forms.TextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents GroupBox5 As System.Windows.Forms.GroupBox
    Friend WithEvents NumericUpDown_Save_Each As System.Windows.Forms.NumericUpDown
    Friend WithEvents NumericUpDown_EXPOSURE As System.Windows.Forms.NumericUpDown
    Friend WithEvents Label23 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label24 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents NumericUpDown_Total_Number_Of_Frames As System.Windows.Forms.NumericUpDown
    Friend WithEvents NumericUpDown_Max_No_Of_Frames_Per_File As System.Windows.Forms.NumericUpDown
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Button_Close As System.Windows.Forms.Button
    Friend WithEvents Button_Start_Acq As System.Windows.Forms.Button
    Friend WithEvents Label_Path As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.TextBox_File_Name = New System.Windows.Forms.TextBox()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.GroupBox5 = New System.Windows.Forms.GroupBox()
        Me.NumericUpDown_Max_No_Of_Frames_Per_File = New System.Windows.Forms.NumericUpDown()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.NumericUpDown_Save_Each = New System.Windows.Forms.NumericUpDown()
        Me.NumericUpDown_EXPOSURE = New System.Windows.Forms.NumericUpDown()
        Me.Label23 = New System.Windows.Forms.Label()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.Label24 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.NumericUpDown_Total_Number_Of_Frames = New System.Windows.Forms.NumericUpDown()
        Me.Button_Close = New System.Windows.Forms.Button()
        Me.Button_Start_Acq = New System.Windows.Forms.Button()
        Me.Label_Path = New System.Windows.Forms.Label()
        Me.GroupBox5.SuspendLayout()
        CType(Me.NumericUpDown_Max_No_Of_Frames_Per_File, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.NumericUpDown_Save_Each, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.NumericUpDown_EXPOSURE, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.NumericUpDown_Total_Number_Of_Frames, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(20, 21)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(74, 17)
        Me.Label5.TabIndex = 14
        Me.Label5.Text = "Output file:"
        '
        'TextBox_File_Name
        '
        Me.TextBox_File_Name.Location = New System.Drawing.Point(100, 18)
        Me.TextBox_File_Name.Name = "TextBox_File_Name"
        Me.TextBox_File_Name.ReadOnly = True
        Me.TextBox_File_Name.Size = New System.Drawing.Size(125, 20)
        Me.TextBox_File_Name.TabIndex = 13
        '
        'Button2
        '
        Me.Button2.Enabled = False
        Me.Button2.Location = New System.Drawing.Point(231, 17)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(24, 21)
        Me.Button2.TabIndex = 12
        Me.Button2.Text = "..."
        '
        'GroupBox5
        '
        Me.GroupBox5.Controls.Add(Me.NumericUpDown_Max_No_Of_Frames_Per_File)
        Me.GroupBox5.Controls.Add(Me.Label2)
        Me.GroupBox5.Controls.Add(Me.NumericUpDown_Save_Each)
        Me.GroupBox5.Controls.Add(Me.NumericUpDown_EXPOSURE)
        Me.GroupBox5.Controls.Add(Me.Label23)
        Me.GroupBox5.Controls.Add(Me.Label6)
        Me.GroupBox5.Controls.Add(Me.Label24)
        Me.GroupBox5.Controls.Add(Me.Label1)
        Me.GroupBox5.Controls.Add(Me.NumericUpDown_Total_Number_Of_Frames)
        Me.GroupBox5.Location = New System.Drawing.Point(14, 71)
        Me.GroupBox5.Name = "GroupBox5"
        Me.GroupBox5.Size = New System.Drawing.Size(241, 152)
        Me.GroupBox5.TabIndex = 57
        Me.GroupBox5.TabStop = False
        Me.GroupBox5.Text = "Measurement Options"
        '
        'NumericUpDown_Max_No_Of_Frames_Per_File
        '
        Me.NumericUpDown_Max_No_Of_Frames_Per_File.Location = New System.Drawing.Point(176, 118)
        Me.NumericUpDown_Max_No_Of_Frames_Per_File.Maximum = New Decimal(New Integer() {10000, 0, 0, 0})
        Me.NumericUpDown_Max_No_Of_Frames_Per_File.Minimum = New Decimal(New Integer() {1, 0, 0, 0})
        Me.NumericUpDown_Max_No_Of_Frames_Per_File.Name = "NumericUpDown_Max_No_Of_Frames_Per_File"
        Me.NumericUpDown_Max_No_Of_Frames_Per_File.Size = New System.Drawing.Size(55, 20)
        Me.NumericUpDown_Max_No_Of_Frames_Per_File.TabIndex = 3
        Me.NumericUpDown_Max_No_Of_Frames_Per_File.Value = New Decimal(New Integer() {400, 0, 0, 0})
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 120)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(173, 27)
        Me.Label2.TabIndex = 46
        Me.Label2.Text = "Max. number of frames in a file:"
        '
        'NumericUpDown_Save_Each
        '
        Me.NumericUpDown_Save_Each.ForeColor = System.Drawing.Color.Black
        Me.NumericUpDown_Save_Each.Location = New System.Drawing.Point(92, 86)
        Me.NumericUpDown_Save_Each.Maximum = New Decimal(New Integer() {1000, 0, 0, 0})
        Me.NumericUpDown_Save_Each.Name = "NumericUpDown_Save_Each"
        Me.NumericUpDown_Save_Each.Size = New System.Drawing.Size(55, 20)
        Me.NumericUpDown_Save_Each.TabIndex = 2
        Me.NumericUpDown_Save_Each.Value = New Decimal(New Integer() {10, 0, 0, 0})
        '
        'NumericUpDown_EXPOSURE
        '
        Me.NumericUpDown_EXPOSURE.DecimalPlaces = 1
        Me.NumericUpDown_EXPOSURE.Enabled = False
        Me.NumericUpDown_EXPOSURE.Location = New System.Drawing.Point(176, 22)
        Me.NumericUpDown_EXPOSURE.Maximum = New Decimal(New Integer() {10000, 0, 0, 0})
        Me.NumericUpDown_EXPOSURE.Name = "NumericUpDown_EXPOSURE"
        Me.NumericUpDown_EXPOSURE.Size = New System.Drawing.Size(55, 20)
        Me.NumericUpDown_EXPOSURE.TabIndex = 12
        Me.NumericUpDown_EXPOSURE.Value = New Decimal(New Integer() {1, 0, 0, 0})
        '
        'Label23
        '
        Me.Label23.Location = New System.Drawing.Point(16, 88)
        Me.Label23.Name = "Label23"
        Me.Label23.Size = New System.Drawing.Size(64, 16)
        Me.Label23.TabIndex = 44
        Me.Label23.Text = "Save each"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(16, 24)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(96, 16)
        Me.Label6.TabIndex = 13
        Me.Label6.Text = "Exposure time:"
        '
        'Label24
        '
        Me.Label24.Location = New System.Drawing.Point(160, 88)
        Me.Label24.Name = "Label24"
        Me.Label24.Size = New System.Drawing.Size(40, 16)
        Me.Label24.TabIndex = 45
        Me.Label24.Text = "frames"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 56)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(154, 18)
        Me.Label1.TabIndex = 2
        Me.Label1.Text = "Total number of frames:"
        '
        'NumericUpDown_Total_Number_Of_Frames
        '
        Me.NumericUpDown_Total_Number_Of_Frames.Location = New System.Drawing.Point(176, 54)
        Me.NumericUpDown_Total_Number_Of_Frames.Maximum = New Decimal(New Integer() {100000, 0, 0, 0})
        Me.NumericUpDown_Total_Number_Of_Frames.Minimum = New Decimal(New Integer() {1, 0, 0, 0})
        Me.NumericUpDown_Total_Number_Of_Frames.Name = "NumericUpDown_Total_Number_Of_Frames"
        Me.NumericUpDown_Total_Number_Of_Frames.Size = New System.Drawing.Size(55, 20)
        Me.NumericUpDown_Total_Number_Of_Frames.TabIndex = 1
        Me.NumericUpDown_Total_Number_Of_Frames.Value = New Decimal(New Integer() {50, 0, 0, 0})
        '
        'Button_Close
        '
        Me.Button_Close.Location = New System.Drawing.Point(173, 242)
        Me.Button_Close.Name = "Button_Close"
        Me.Button_Close.Size = New System.Drawing.Size(72, 24)
        Me.Button_Close.TabIndex = 59
        Me.Button_Close.Text = "Close"
        '
        'Button_Start_Acq
        '
        Me.Button_Start_Acq.Location = New System.Drawing.Point(33, 242)
        Me.Button_Start_Acq.Name = "Button_Start_Acq"
        Me.Button_Start_Acq.Size = New System.Drawing.Size(72, 24)
        Me.Button_Start_Acq.TabIndex = 58
        Me.Button_Start_Acq.Text = "Start acq"
        '
        'Label_Path
        '
        Me.Label_Path.AutoSize = True
        Me.Label_Path.Font = New System.Drawing.Font("Microsoft Sans Serif", 6.5!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label_Path.Location = New System.Drawing.Point(98, 41)
        Me.Label_Path.Name = "Label_Path"
        Me.Label_Path.Size = New System.Drawing.Size(86, 12)
        Me.Label_Path.TabIndex = 60
        Me.Label_Path.Text = "Label3Label3Label3"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(265, 278)
        Me.Controls.Add(Me.Label_Path)
        Me.Controls.Add(Me.Button_Close)
        Me.Controls.Add(Me.Button_Start_Acq)
        Me.Controls.Add(Me.GroupBox5)
        Me.Controls.Add(Me.Label5)
        Me.Controls.Add(Me.TextBox_File_Name)
        Me.Controls.Add(Me.Button2)
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Aquisition program"
        Me.GroupBox5.ResumeLayout(False)
        CType(Me.NumericUpDown_Max_No_Of_Frames_Per_File, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.NumericUpDown_Save_Each, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.NumericUpDown_EXPOSURE, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.NumericUpDown_Total_Number_Of_Frames, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

#End Region



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Filling NumericUpDown fields with the settings from WinSpec
        Dim Acq_Setup As New ExpSetup
        TextBox_File_Name.Text = Path.GetFileNameWithoutExtension(Acq_Setup.GetParam(EXP_CMD.EXP_DATFILENAME))
        Label_Path.Text = Path.GetDirectoryName(Acq_Setup.GetParam(EXP_CMD.EXP_DATFILENAME))
        NumericUpDown_EXPOSURE.Value = Acq_Setup.GetParam(EXP_CMD.EXP_EXPOSURE)
        Acq_Setup.GetParam(EXP_CMD.EXP_SEQUENTS)
    End Sub

    Private Sub Button_Start_Acq_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Start_Acq.Click

        Dim Acq_Setup As New ExpSetup

        'Saving WinSpec primary settings to be restored after the acquisition
        Dim Primary_File_Access_Mode As Integer = Acq_Setup.GetParam(EXP_CMD.EXP_FILEACCESS)
        Dim Primary_EXPOSURE As Integer = Acq_Setup.GetParam(EXP_CMD.EXP_EXPOSURE)

        Acq_Setup.SetParam(EXP_CMD.EXP_EXPOSURE, NumericUpDown_EXPOSURE.Value)
        Acq_Setup.SetParam(EXP_CMD.EXP_SEQUENTS, NumericUpDown_Save_Each.Value)
        Acq_Setup.SetParam(EXP_CMD.EXP_AUTOSAVE, 2) 'After single aquisition is finished save automatically without asking for confirmation

        Acquisition1(NumericUpDown_Total_Number_Of_Frames.Value, NumericUpDown_Max_No_Of_Frames_Per_File.Value, NumericUpDown_Save_Each.Value, _
                    Label_Path.Text, TextBox_File_Name.Text, Acq_Setup)
   
        'Restoring WinSpec primary settings
        Acq_Setup.SetParam(EXP_CMD.EXP_FILEACCESS, Primary_File_Access_Mode)
        Acq_Setup.SetParam(EXP_CMD.EXP_EXPOSURE, Primary_EXPOSURE)

    End Sub

    Private Sub Button_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Close.Click
        Application.Exit()
    End Sub


    Private Sub NumericUpDown_Total_Number_Of_Frames_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown_Total_Number_Of_Frames.Enter
        NumericUpDown_Total_Number_Of_Frames.Select(0, NumericUpDown_Total_Number_Of_Frames.Value.ToString.Length)
    End Sub
End Class